package pers.vicxu.mvcrestplus.controller;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.*;
import pers.vicxu.mvcrestplus.pojo.Book;
import pers.vicxu.mvcrestplus.dto.ResponseMsg;
import pers.vicxu.mvcrestplus.service.BookService;

import java.util.List;


/**
 * Create by Vic Xu on 7/25/2018
 *
 * @author Vic Xu
 */
@CrossOrigin(origins = "*", maxAge = 3600)
@RestController
@RequestMapping("/book")
public class BookController {

    @Autowired
    @Qualifier(value = "BookServiceImpl")
    private BookService bookService;

    @GetMapping("all")
    public List<Book> findAll() {
        return bookService.findAll();
    }

    @PostMapping(value = "add", consumes = "application/json")
    public ResponseMsg addBook(@RequestBody Book book) {
        Book book1 = bookService.addBook(book);
        if (book1 == null) {
            return new ResponseMsg("failed");
        }
        return new ResponseMsg("success", book);
    }

    @DeleteMapping(value = "delete", consumes = "application/json")
    public ResponseMsg deleteBook(@RequestBody Book book) {
        if (bookService.deleteBook(book.getId())) {
            return new ResponseMsg("success");
        }
        return new ResponseMsg("failed");
    }

    @PutMapping(value = "update", consumes = "application/json")
    public ResponseMsg updateBook(@RequestBody Book book) {
        Book book1 = bookService.updateBook(book);
        return new ResponseMsg("success", book1);
    }

}
